/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vng.zing.stdprofile2.client;

import com.vng.jcore.thriftutil.TClientPool;
import com.vng.jcore.thriftutil.TClientPoolConfig;
import com.vng.jcore.thriftutil.TClientPoolMan;
import com.vng.zing.lib.Config;
import com.vng.zing.stdprofile2.thrift.StdProfile2Service_Rd;
import com.vng.zing.stdprofile2.thrift.TValueResult;
import com.vng.zing.zcommon.thrift.OpHandle;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.cliffc.high_scale_lib.NonBlockingHashMap;

/**
 *
 * @author kienpd
 */
public class StdpofileClient {
    
    private static final Lock createLock_ = new ReentrantLock();
    private static final Map<String, StdpofileClient> _instances = new NonBlockingHashMap();
    private static final String host =Config.getParamStr("profile_server", "host");
    private static final int port = Config.getParamInt("profile_server", "port");
    private TClientPool<StdProfile2Service_Rd.Client> Pool;

    public static StdpofileClient getInstance() {
        String key = String.format("%s:%s", new Object[]{host, port});

        if (!_instances.containsKey(key)) {
            createLock_.lock();
            try {
                if (_instances.get(key) == null) {
                    _instances.put(key, new StdpofileClient());
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instances.get(key);
    }

    private StdpofileClient() {
        TClientPoolConfig.ConnConfig conn_cfg = new TClientPoolConfig.ConnConfig(host, port, true, false, 0);

        // get current pool
        Pool = (TClientPool<StdProfile2Service_Rd.Client>) TClientPoolMan.Instance.getPool(host, port, StdProfile2Service_Rd.Client.class);
        if (Pool == null) {
            // add client to pool
            TClientPoolMan.Instance.addPool(
                    new TClientPool<StdProfile2Service_Rd.Client>(
                            new StdProfile2Service_Rd.Client.Factory(), conn_cfg, TClientPoolConfig.DefaultPoolConfig));
            // get current pool
            Pool = (TClientPool<StdProfile2Service_Rd.Client>) TClientPoolMan.Instance.getPool(host, port, StdProfile2Service_Rd.Client.class);
        }
        assert (Pool != null);

    }
     public TValueResult getByName(OpHandle handle,String name) throws Exception {
        TValueResult result;
        StdProfile2Service_Rd.Client cli = null;
        try {
            // get client
            cli = Pool.borrowClient();
            assert (cli != null);
            result = cli.getByName(handle,name);
            //Return client back to pool
            Pool.returnObject(cli);
        } catch (Exception ex) {
            Pool.invalidObjExless(cli);
            throw ex;
        }
        return result;

    }
}
